package com.example.springrabbitmq.listener;

import com.example.springrabbitmq.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RetryListener {
    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void listener(Message message) throws IOException {
        System.out.println("消息内容：" + new String(message.getBody()));
        System.out.println("deliverTag" + message.getMessageProperties().getDeliveryTag());
        // 模拟消息处理失败
        int num = 1 / 0;
        System.out.println("处理完成");
    }


//    @RabbitListener(queues = Constants.RETRY_QUEUE)
//    public void listener(Message message, Channel channel) throws IOException {
//        System.out.println("消息内容：" + new String(message.getBody()));
//        System.out.println("deliverTag" + message.getMessageProperties().getDeliveryTag());
//        try {
//            // 模拟处理失败
//            int num = 1 / 0;
//            System.out.println("处理完成");
//            // 手动签收
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
//        } catch (Exception e) {
//            // 异常情况下拒绝签收，并将消息重新入队
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
//        }
//    }
}
